﻿@function breakpoint-min($name, $breakpoints: $grid-breakpoints) {
    $min: map-get($breakpoints, $name);
    @return if($min != 0, $min, null);
}

@function breakpoint-infix($name, $breakpoints: $grid-breakpoints) {
    @return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
}

@function color-yiq($color, $dark: $yiq-text-dark, $light: $yiq-text-light) {
    $r: red($color);
    $g: green($color);
    $b: blue($color);

    $yiq: (($r * 299) + ($g * 587) + ($b * 114)) / 1000;

    @if ($yiq >= $yiq-contrasted-threshold) {
        @return $dark;
    }
    @else {
        @return $light;
    }
}

@function theme-color($key: "primary") {
    @return map-get($theme-colors, $key);
}

@function theme-color-level($color-name: "primary", $level: 0) {
    $color: theme-color($color-name);
    $color-base: if($level > 0, $black, $white);
    $level: abs($level);

    @return mix($color-base, $color, $level * $theme-color-interval);
}

@function max($v1, $v2) {
    @return if($v1 > $v2, $v1, $v2);
}

@function min($v1, $v2) {
    @return if($v1 < $v2, $v1, $v2);
}

@function hsv_to_hsl($h, $s: 0, $v: 0) {
    @if type_of($h) == 'list' {
        $v: nth($h, 3);
        $s: nth($h, 2);
        $h: nth($h, 1);
    }

    @if unit($h) == 'deg' {
        $h: 3.1415 * 2 * ($h / 360deg);
    }

    @if unit($s) == '%' {
        $s: 0 + ($s / 100%);
    }

    @if unit($v) == '%' {
        $v: 0 + ($v / 100%);
    }

    $ss: $s * $v;
    $ll: (2 - $s) * $v;

    @if $ll <= 1 {
        $ss: $ss / $ll;
    }
    @else if ($ll == 2) {
        $ss: 0;
    }
    @else {
        $ss: $ss / (2 - $ll);
    }

    $ll: $ll / 2;

    @return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $ss))), percentage(max(0, min(1, $ll)));
}

@function hsl_to_hsv($h, $ss: 0, $ll: 0) {
    @if type_of($h) == 'list' {
        $ll: nth($h, 3);
        $ss: nth($h, 2);
        $h: nth($h, 1);
    }
    @else if type_of($h) == 'color' {
        $ll: lightness($h);
        $ss: saturation($h);
        $h: hue($h);
    }

    @if unit($h) == 'deg' {
        $h: 3.1415 * 2 * ($h / 360deg);
    }

    @if unit($ss) == '%' {
        $ss: 0 + ($ss / 100%);
    }

    @if unit($ll) == '%' {
        $ll: 0 + ($ll / 100%);
    }

    $ll: $ll * 2;

    @if $ll <= 1 {
        $ss: $ss * $ll;
    }
    @else {
        $ss: $ss * (2 - $ll);
    }

    $v: ($ll + $ss) / 2;
    $s: (2 * $ss) / ($ll + $ss);

    @return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $s))), percentage(max(0, min(1, $v)));
}

@function color_to_hsv($color) {
    @return hsl_to_hsv($color);
}

@function hsv_to_color($h, $s: 0, $v: 0) {
    $hsl: hsv_to_hsl($h, $s, $v);
    @return hsl(nth($hsl, 1), nth($hsl, 2), nth($hsl, 3));
}